﻿// **********************************************
// SharePoint WebPart: UsersADBrowser
// Author: Khristoforov Yuri
// WWW   : http://usersadbrowser.codeplex.com, http://activex.net.ru
// **********************************************

using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace UsersADBrowser.WebParts
{
    [XmlRoot(Namespace = "UsersADBrowser")]
    public abstract class WebPartTemplater : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private const string _wpTmplPath = @"~/_controltemplates/";
        private string _wpDirName = null;
        private string _templateFileName = null;
        private SPWebPartManager _wpManager = null;
        private string _exception = "";

        [Browsable(false)]
        public string WPDirName
        {
            get
            {
                return _wpDirName;
            }
            set
            {
                _wpDirName = value;
            }
        }

        [Browsable(false)]
        public string TemplateFileName
        {
            get
            {
                return _templateFileName;
            }
            set
            {
                _templateFileName = value;
            }
        }

        [Browsable(false)]
        public string TemplateRootPath
        {
            get
            {
                return _wpTmplPath;
            }
        }

        [Browsable(false)]
        public string WPException
        {
            get
            {
                return _exception;
            }
            set
            {
                _exception = value;
            }
        }

        [Browsable(false)]
        protected SPWebPartManager CurrentWebPartManager
        {
            get
            {
                return _wpManager;
            }
        }

        protected override void CreateChildControls()
        {
            _wpManager = (SPWebPartManager)SPWebPartManager.GetCurrentWebPartManager(this.Page);

            Control ctl = LoadWPTemplate();
            if (ctl != null)
            {
                InitControls(ctl);
            }
        }

        protected Control LoadWPTemplate()
        {
            Control ctl = null;
            try
            {
                string wpDir = this.Page.Request.MapPath(_wpTmplPath);
                string[] arrTestEx = null;
                try
                {
                    arrTestEx = Directory.GetFiles(wpDir);
                }
                catch (Exception ex)
                {
                    WPException += String.Format("No access to templates directory : {0}", ex.Message.ToString());
                    return null;
                }
                ctl = this.Page.LoadControl(_wpTmplPath + _wpDirName + "/" + _templateFileName);
                Controls.Add(ctl);
            }
            catch (Exception ex)
            {
                WPException += "Cannot load controls in WebPart: " + ex.Message.ToString();
                return null;

            }
            finally
            {
                base.CreateChildControls();
            }
            return ctl;
        }

        protected Control GetControl(Control ctl, string controlID)
        {
            Control ctlFoundControl = ctl.FindControl(controlID);
            return ctlFoundControl;
        }

        protected abstract void InitControls(Control ctrl);

        protected override void RenderContents(HtmlTextWriter writer)
        {
            try
            {
                base.RenderContents(writer);
            }
            catch (Exception ex)
            {
                WPException += "Error rendering WebPart: " + ex.Message.ToString();
            }
            finally
            {
                if (WPException.Length > 0)
                {
                    writer.WriteLine("<font color=red><b>Error: " + WPException + "</b></font>");
                }
            }
        }
    }
}
